package com.weilai.service.serviceImpl;

import com.weilai.controller.exceptionHandler.BusinessException;
import com.weilai.controller.exceptionHandler.Code;
import com.weilai.dao.UserDao;
import com.weilai.domain.User;
import com.weilai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    //查询所有用户
    @Override
    public List<User> getAllUsers() {
        try {
            return userDao.getAllUsers();
        } catch (Exception e) {
            throw new BusinessException(Code.SYSTEM_DB_ERROR, "查询所有用户时数据库出错");
        }
    }

    //按id查询用户
    @Override
    public User getUserById(int id) {
        try {
            return userDao.getById(id);
        } catch (Exception e) {
            throw new BusinessException(Code.SYSTEM_DB_ERROR, "按id查询用户时数据库出错");
        }
    }

    //按名字查询用户
    @Override
    public User getUserByUsername(String username){
        try {
            return userDao.getByUsername(username);
        } catch (Exception e) {
            throw new BusinessException(Code.SYSTEM_DB_ERROR, "按用户名查询用户时数据库出错");
        }
    }

    //按邮箱查找用户
    @Override
    public User getUserByEmail(String email) {
        try {
            return userDao.getByEmail(email);
        } catch (Exception e) {
            throw new BusinessException(Code.SYSTEM_DB_ERROR, "按邮箱查询用户时数据库出错");
        }
    }

    //用户注册
    @Override
    public void insertUser(User user) {
        try {
            userDao.insertUser(user);
        } catch (Exception e) {
            throw new BusinessException(Code.SYSTEM_DB_ERROR, "用户注册时数据库出错");
        }
    }

    //修改用户信息
    @Override
    public void updateUser(User user) {
        try {
            userDao.updateUser(user);
        } catch (Exception e) {
            throw new BusinessException(Code.SYSTEM_DB_ERROR, "修改用户信息时数据库出错");
        }
    }

    // 按 id 删除用户信息
    @Override
    public boolean deleteUserById(int id) {
        try {
            boolean flag = false;
            if (userDao.deleteUserById(id) > 0) {
                flag = true;
            }
            return flag;
        } catch (Exception e) {
            throw new BusinessException(Code.SYSTEM_DB_ERROR, "按id删除用户信息时数据库出错");
        }
    }

    //查询用户名是否存在
    @Override
    public Boolean checkUsernameExists(String username) {
        try {
            boolean flag = false;
            if (userDao.selectUserName(username) != null) {
                flag = true;
            }
            return flag;
        } catch (Exception e) {
            throw new BusinessException(Code.SYSTEM_DB_ERROR, "查询用户名是否存在时数据库出错");
        }
    }

    //根据用户名关键字查询
    @Override
    public List<User> selectByKeywords(String Keywords) {
        try {
            List<User> users = userDao.selectByKeywords(Keywords);
            if (users == null || users.isEmpty()) {
                return new ArrayList<>();
            }
            return users;
        } catch (Exception e) {
            throw new BusinessException(Code.SYSTEM_DB_ERROR, "根据用户名关键字查询时数据库出错");
        }
    }


}
